package com.android.fragmentdemo.viewpager2
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import kotlin.math.abs

class ViewPager2Transformer : ViewPager2.PageTransformer {

    override fun transformPage(page: View, position: Float) {
        page.apply {
            rotationY = when {
                position <= -1 -> {
                    translationX = (width * -position) / 20
                    alpha = 0.8F
                    10.0F
                }
                position > 1 -> {
                    alpha = 0F
                    -10.0F
                }
                else -> {
                    translationX = (width * -position) / 20
                    alpha = abs(1 - position / 1.0f).coerceIn(0.8f..1.0f)
                    position / 1.0f * (-10.0f)
                }
            }
        }
    }
}